package com.shujia.wyh.day08;

/*
    按字典顺序比较两个字符串
    int compareTo(String str)
    int compareToIgnoreCase(String str)

 */
public class StringDemo9 {
    public static void main(String[] args) {
        String s1 = "hello"; // 104
        String s2 = "world"; // 119
        System.out.println(s1.compareTo(s2)); // -15

        String s3 = "hel";
        System.out.println(s1.compareTo(s3)); // 2
    }
}
/*
        public int compareTo(String anotherString) {
        //this -- s1 -- "hello" -- ['h','e','l','l','o']
        //anotherString -- s2 -- "hel" -- ['h','e','l']
        int len1 = this.value.length; // 5
        int len2 = anotherString.value.length; // 3
        int lim = Math.min(len1, len2); // 3
        char[] v1 = value; // ['h','e','l','l','o']
        char[] v2 = anotherString.value; // ['h','e','l']

        int k = 0;
        while (k < lim) {
            char c1 = v1[k]; // h e l
            char c2 = v2[k]; // h e l
            if (c1 != c2) {
                return c1 - c2;  // 104-119 = -15
            }
            k++;
        }
        return len1 - len2; //5-3=2
    }

 */
